#include "ZigbeeNewControlProtocol.h"

ZigbeeNewControlProtocol::ZigbeeNewControlProtocol(QObject *parent) :
    BaseProtocol(parent)
{
    this->m_protocolType = ProtocolType::ZigbeeNewControlProtocol;

}


QList<Package> ZigbeeNewControlProtocol::parseData(QByteArray data)
{
    quint8  checkSteps = CHECK_HEAD;          //当前解析状态
    quint8 headnum;
    quint8 functionData;
    quint16 lengthCount=0;
    quint16 length=0;
    quint16 bodyLength=0;
    quint16 clusterID=0;
    QByteArray dataArray;
    QByteArray getID;
    QList<Package> packageList;
    packageList.clear();
//    qDebug()<<"ZIGPARSE";
    for(int i=0; i<data.size(); i++)
    {
        quint8 byte = data.at(i);

       // qDebug()<<byte;
        switch(checkSteps)
        {
            case CHECK_HEAD:   //检查数据头
                functionData=0;
                lengthCount = 0;
                bodyLength = 0;
                length=0;
                headnum=i;
                dataArray.clear();
                getID.clear();
//                qDebug()<<"Stata-Head="<<byte;;
                 if(byte == NewJoinData)
                 {
                    dataArray.append(NewJoinData);
                    functionData=NewJoinData;
                    checkSteps = CHECK_GetShortID;
               //     qDebug()<<"A1";
                 }
                 else if(byte ==StateData)
                 {
                     dataArray.append(StateData);
                     functionData=StateData;
                     checkSteps = CHECK_GetShortID;
//                    qDebug()<<"Stata-StateData";
                 }
                 else if(byte ==QuitData)
                 {
                     dataArray.append(QuitData);
                     functionData=QuitData;
                     checkSteps = CHECK_GetShortID;
                 }
            break;

            case CHECK_GetShortID:

                  dataArray.append(byte);
                 if(functionData==NewJoinData)
                 {
                     if(i==headnum+6)
                     {

                         getID.append(byte);
                     }
                     if(i==headnum+7)
                     {
                         getID.append(byte);
                        qDebug()<<"Stata=getShortID="<<getID;
                         checkSteps = ChECK_GetLongID;
                     }
                 }
                else if(functionData==StateData)
                 {
                     if(i==headnum+1)
                     {
                         getID.append(byte);
                     }
                     if(i==headnum+2)
                     {
                         getID.append(byte);
                         qDebug()<<"Stata=getShortID="<<getID;
                         checkSteps = CHECK_GetClusterID;
                     }
                 }
                 else if(functionData==QuitData)
                  {
                      if(i==headnum+1)
                      {
                          getID.append(byte);
                      }
                      if(i==headnum+2)
                      {
                          getID.append(byte);
                          checkSteps = ChECK_GetLongID;
                      }
                  }
                 break;

            case ChECK_GetLongID:
                if(functionData==NewJoinData)
                {
                 dataArray.append(byte);
                 getID.append(byte);
                 if(i==headnum+15)
                 {
                 checkSteps = CHECK_EndForNewNet;
                 }
                }
                else if(functionData==QuitData)
                {
                    dataArray.append(byte);
                    getID.append(byte);
                    if(i==headnum+10)
                    {
                    checkSteps = CHECK_END;
                    }
                }
            break;

            case CHECK_GetClusterID:
                  dataArray.append(byte);

                 if(i==headnum+4)
                 {
                     clusterID=byte*256;
                 }
                 else if(i==headnum+5)
                 {
                     clusterID|=byte;
                    qDebug()<<"getclusterID="<<clusterID;
                    if(clusterID==0x0005||0x0000)
                    {
                     checkSteps = CHECK_GetDataLength;
                     qDebug()<<"go to GetDataLength1";
                    }
                    else if(clusterID==0x0000)
                    {
                     checkSteps = CHECK_GetDataLength;
                     qDebug()<<"go to GetDataLength2";
                    }
                    else
                    {
                    checkSteps=CHECK_HEAD;
                    }
                 }

            break;
            case CHECK_GetDataLength:
            dataArray.append(byte);
                if(byte<data.size())
                {
                    lengthCount = byte;
                    qDebug()<<"State=lengthCount="<<lengthCount;
                    checkSteps = CHECK_GetData;
                }
                else
                {
                    qDebug()<<"getDataLength="<<lengthCount;
                    checkSteps = CHECK_HEAD;
                }


            case CHECK_GetData:
                if(length!=lengthCount)
                {

                   dataArray.append(byte);
                   length++;
               //   qDebug()<<"length="<<length<<",data="<<byte;
                   break;
                }
                else
                {
                    qDebug()<<"lengthCOunt"<<lengthCount;
                    checkSteps = CHECK_END;
                }



            case CHECK_EndForNewNet:
                 dataArray.append(byte);
                 if(i==headnum+17)
                 {
                    qDebug()<<"now EndForNewNet="<<byte;
                     if(byte==0x03)
                     {
                         Package package;
                         package.setHead(dataArray.mid(headnum,1));
                         //package.setFunction(dataArray.mid(headnum,1).toHex().toInt(0,16));
                         package.setFunction(functionData);
                         package.setBody(getID);
                         packageList.append(package);

                    //     qDebug()<<getID.toHex().data();
                     //    qDebug()<<package.body().toHex().data();
                     //    qDebug()<<package.function();
                     }
                     checkSteps = CHECK_HEAD;
                 }
            break;

            case CHECK_END:
            qDebug()<<"CHECK END="<<byte;
            if(byte==0x15)
            {
                dataArray.append(byte);
                qDebug()<<"CHECK END ";
                Package package;
                package.setHead(data.mid(headnum,1));
              //  package.setFunction(dataArray.mid(headnum,1).toHex().toInt(0,16));
                package.setFunction(functionData);
                package.setBody(dataArray);
                packageList.append(package);
        //        qDebug()<<getID.toHex().data();
       //         qDebug()<<package.body().toHex().data();
       //         qDebug()<<package.function();
                checkSteps = CHECK_HEAD;
            }
            else if(byte==0x14)
            {
                dataArray.append(byte);
                qDebug()<<"CHECK END ";
                Package package;
                package.setHead(dataArray.mid(headnum,1));
                package.setFunction(dataArray.mid(headnum,1).toHex().toInt(0,16));
                package.setBody(dataArray);
                packageList.append(package);
                checkSteps = CHECK_HEAD;
            }
            else
            {
                checkSteps = CHECK_HEAD;
            }

            break;

        }
    }
    return packageList;
}

QByteArray ZigbeeNewControlProtocol::generateData(Package package)
{
    QByteArray data;
    quint8 length = package.body().size();

    data.append(0x7C);
    data.append(length);
    data.append(0x01);
    data.append(DataTypeConver::intToByteArray(package.function(),1));
    data.append(package.body());
    quint8 checkcode = CheckCode::computeSum(data,0,length+4);
    data.append(checkcode);

    return data;
}
